/**
************************************************************
* @file         soil.c
* @brief        soil driver
* @author       Gizwits
* @date         2018-07-19
* @version      V03030000
* @copyright    Gizwits
*
* @note         机智云.只为智能硬件而生
*               Gizwits Smart Cloud  for Smart Products
*               链接|增值ֵ|开放|中立|安全|自有|自由|生态
*               www.gizwits.com
*
***********************************************************/
#include "soil.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>

#include "qapi/qapi.h"
#include "qapi/qapi_status.h"

#include "qapi/qapi_gpioint.h"
#include "qapi/qapi_tlmm.h"
#include "qapi/qapi_types.h"
#include "qapi/qapi_fs.h"

#include "qapi/qapi_adc.h"
#include "qapi/qapi_adc_types.h"

#define FAIL    -1
#define SUCCESS  0
#define GPIO_THREAD_EVENT_MASK                          (0x00000001)



static qapi_ADC_Handle_t handle;
static qapi_Adc_Input_Properties_Type_t Properties; 

char * soil_demo_error_to_string(qapi_Status_t status)
{
    char * err_string;
    switch (status)
    {
        case QAPI_ERROR:
            err_string = "Call failed";
            break;
        case QAPI_ERR_INVALID_PARAM:
            err_string = "Invalid parameters were specified";
            break;
        case QAPI_ERR_NO_MEMORY:
            err_string = "No memory available to support this operation";
            break;
        case QAPI_ERR_NO_RESOURCE:
            err_string = "No more handles are available";
            break;
        default:
            err_string = "UNKNOWN";
            break;
    }
    return err_string;
}

int32_t soilGetValue(void)
{
    int32_t value = 0;
    qapi_Status_t status; 
    qapi_ADC_Read_Result_t result; 

    status = qapi_ADC_Read_Channel(handle, &Properties, &result);
    if (status != QAPI_OK)
    {
        iofPrintf("read channel failed !!! %s\n", soil_demo_error_to_string(status));
    }

    value = result.nMicrovolts / 1000; //result mV  0~3V,(x/1000/1000/3)*100
    
    return value;
}

void soilInit(void)
{
    qapi_Status_t status; 

    int8_t * Channel_Name = NULL;
    uint32_t Channel_Name_Size = 0;
    uint32_t id = 0;

    status = qapi_ADC_Open(&handle, id);
    if (status != QAPI_OK)
    {
        iofPrintf("open adc failed !!! %s\n", soil_demo_error_to_string(status));
        return;
    }

    Channel_Name = "MPP4_ADC0";
    Channel_Name_Size = strlen((const char *)Channel_Name);
    status = qapi_ADC_Get_Input_Properties(handle, Channel_Name, Channel_Name_Size, &Properties);
    if (status != QAPI_OK)
    {
        iofPrintf("get input Properties failed !!! %s\n", soil_demo_error_to_string(status));
    }
    
    return;
}

void soilExit(void)
{
    qapi_Status_t      status;

    if (handle == NULL)
    {
        iofPrintf("ADC not open, please open it\n");
        return;
    }

    status = qapi_ADC_Close(handle, false);
    if (status != QAPI_OK)
    {
        iofPrintf("close adc failed !!! %s\n", soil_demo_error_to_string(status));
    }
    else
    {
        handle = NULL;
    }

    return;
}